package com.ymttest.database.sqlwapper;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtProducts;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.utils.EnvSetup;

/************************************************************************************
 * @File name : YmtUserWapper.java
 * @Author : zhouyi
 * @Date : 2015年4月2日
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtProductsWapper {
	SqlManager manage;

	public YmtProductsWapper() {
		manage = new SqlManager("ymt" + EnvSetup.getEnv());
	}

	/**
	 * 获取userid对应的一个最新发布的商品信息，iaction>=0
	 * 
	 * @param userid
	 * @return YmtProducts
	 * @throws InterruptedException
	 */
	public YmtProducts selectByiUserId(String userid) throws InterruptedException {
		List<YmtProducts> userList = manage.getSelectResults(
				"com.ymttest.database.mapping.YmtProductsMapper.selectByiUserId", userid);

		// manage.close();
		return userList.get(0);
	}

	/**
	 * 更新
	 * 
	 * @param date
	 * @param count
	 * @param YmtProducts
	 *            对象 包含primarykey
	 * @return update 数
	 * @throws InterruptedException
	 */
	public int updateByPK(String sProductId, int iSellNum, Date validEnd) throws InterruptedException {
		Map m = new HashMap();
		m.put("sProductId", sProductId);
		m.put("iSellNum", iSellNum);
		m.put("validEnd", validEnd);
		int i = manage.update("com.ymttest.database.mapping.YmtProductsMapper.updateByPK", m);
		manage.commit();
		// manage.close();
		return i;
	}

	/**
	 * insert 一条insertProducts记录
	 * 
	 * @param record
	 *            insertProducts
	 * @return
	 * @throws InterruptedException
	 */
	public int insertProducts(YmtProducts record) {
		int i = manage.insert("com.ymttest.database.mapping.YmtProductsMapper.insert", record);
		manage.commit();
		// manage.close();
		return i;
	}

	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}
	
	//根据商品id查询
	public List<YmtProducts> selectProductByProductId(String sProductId) throws InterruptedException {
	    @SuppressWarnings("unchecked")
	    List<YmtProducts>  sProductIdlist = manage.getSelectResults(
	            "com.ymttest.database.mapping.YmtProductsMapper.selectProductByProductId", sProductId);
	    manage.commit();
	    return sProductIdlist;
	}

	public static void main(String args[]) {
		try {
			// Logger.debug(new
			// YmtProductsWapper().selectByiUserId("3383").getSproductid());
//			Calendar calendar = Calendar.getInstance();
//			calendar.add(Calendar.DAY_OF_MONTH, 1);
//			Logger.debug(new YmtProductsWapper().updateByPK("3BEA5046-63B2-4C30-B9C9-BF3C31894BDE", 100,
//					calendar.getTime()));
	    	YmtProductsWapper ymtProductsWapper = new YmtProductsWapper();  
	        List<YmtProducts> rm = ymtProductsWapper.selectProductByProductId("6bede0f6-7641-4474-9ba9-d83adead529d");
	        Logger.debug(rm);
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
